﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.Composition;
using Kooboo.Commerce.AddIn;


namespace Kooboo.Commerce.Validators
{
    [Export(typeof(IValidator))]
    public class GreaterThan : IValidator
    {
        #region IValidator Members

        public string Name
        {
            get
            {
                return "GreaterThan";
            }
        }

     


        #endregion

        #region IValidator Members


        public bool Validate(string value, List<ValidatorParameter> args)
        {
  
            var minimum = args.Where(i => i.Name == "MinValue").Select(i => i.Value).FirstOrDefault();

            if (string.IsNullOrWhiteSpace(minimum) == false)
            {
                decimal number;
                DateTime date;
                if (decimal.TryParse(minimum, out number))
                {
                    try
                    {
                        if (number > decimal.Parse(value))
                        {
                            return false;
                        }
                    }
                    catch
                    {
                        return false;
                    }
                }
                else if (DateTime.TryParse(minimum, out date))
                {
                    try
                    {
                        if (date > DateTime.Parse(value))
                        {
                            return false;
                        }
                    }
                    catch
                    {
                        return false;
                    }
                }
                else
                {
                    if (minimum == value)
                    {
                        return false;
                    }
                    else
                    {
                        List<string> values = (new List<string>()
                        {
                            minimum,
                            value
                        })
                        .OrderBy(i => i)
                        .ToList();

                        if (values[1] == minimum)
                        {
                            return false;
                        }
                    }
                }
            }

            return true;
           
        }

        public List<ValidatorParameter> GetParameters()
        {
            return new List<ValidatorParameter>()
            {
                new ValidatorParameter{ Name = "MinValue", Example = "(100)"}
            };
        }

        #endregion
    }
}